$(function () {
    if (window.history && window.history.pushState) {
        $(window).on('popstate', function () {
            window.history.pushState('forward', null, '#');
            window.history.forward(1);
        });
    }
    window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
    window.history.forward(1);

    $('[data-toggle="popover"]').popover()
    if (window !== top) {
        top.location.href = location.href;
    }

    $(document).on("keydown", "body,#inputUsername,#inputPassword,#inputVerifyCode", function (evt) {
        evt.stopPropagation();
        var e = evt || event;
        if (e.keyCode === 13) {
            if (!$(this).is("#inputUsername,#inputPassword,#inputVerifyCode")) {
                login();
            }
        }
    })
    $("#loginFrom").submit(function (e) {
        e.preventDefault();
    });
    $("#submitBtn").on("click", function () {
        login();
    })
    $("#inputUsername").on("input propertychange change", function (e) {
        validateName = false;
        var username = $(this).val().trim();
        if (isEmpty(username)) {
            $(this).attr("data-content", "请填写用户名").popover('show');
        } else if (!/^[\w\u4e00-\u9fa5]+$/.test(username)) {
            $(this).attr("data-content", "请输入字母数字下划线和汉字").popover('show');
        } else if (getChineseLen(username) > 20) {
            $(this).attr("data-content", "用户名长度不得大于20,汉字占2长度").popover('show');
        } else {
            $(this).popover('hide');
            validateName = true;
        }
    })
    $("#inputPassword").on("input propertychange change", function (e) {
        validatePassword = false;
        var password = $(this).val().trim();
        if (isEmpty(password)) {
            $(this).attr("data-content", "请填写密码").popover('show');
        } else if (!/^[a-zA-Z0-9~!@#$%^&*,./?_]+$/.test(password)) {
            $(this).attr("data-content", "只能包含字母、数字以及标点符号").popover('show');
        } else if (getChineseLen(password) > 16) {
            $(this).attr("data-content", "密码长度不能大于16").popover('show');
        } else if (getChineseLen(password) < 6) {
            $(this).attr("data-content", "密码长度不能小于6").popover('show');
        } else {
            $(this).popover('hide');
            validatePassword = true;
        }
    })
    $("#inputVerifyCode").on("input propertychange change", function (e) {
        validateCode = false;
        var code = $(this).val().trim();
        if (isEmpty(code)) {
            $(this).attr("data-content", "请填写验证码").popover('show');
        } else {
            $(this).popover('hide');
            validateCode = true;
        }
    })
    var $inputPassword = $("#inputPassword");
    $("#wordbt").on("click", function () {
        if ($inputPassword.is(":password")) {
            $(this).css("background-image", "../images/wd1.png");
            $inputPassword.attr("type", "text");
        } else {
            $(this).css("background-image", "../images/wd2.png");
            $inputPassword.attr("type", "password");
        }
    })
    //verifyCode
    $("#verifyCode").click(function () {
        $("#verifyCode").attr('src', 'verifyCode.do?t=' + Math.random())
    })
});
var validatePassword, validateName, validateCode;

function login() {
    if ($(".bootbox-alert").length > 0) {
        return false;
    }
    $("#inputUsername,#inputPassword,#inputVerifyCode").change();
    if (!validatePassword || !validateName|| !validateCode) {
        return;
    }
    var password = $.trim($("#inputPassword").val());
    var username = $.trim($("#inputUsername").val());
    var code = $.trim($("#inputVerifyCode").val());
    $.ajax({
        url: "login.do",
        type: "post",
        async: false,
        data: "username=" + username + "&password=" + hex_md5(password) + "&code=" + code,
        dataType: "json",
        success: function (data) {
            if (data["errorCode"] == "0") {
                window.location.href = "toMain.do";
            } else {
                $("#verifyCode").attr('src', 'verifyCode.do?t=' + Math.random())
                if (data["errorCode"] == "15004") {
                    bootbox.alert("未分配业务模板，请联系领导分配模板");
                } else if(data["errorCode"] == "2005"){
                    bootbox.alert("该账户所在组织已经关闭");
                } else if(data["errorCode"] == "2006"){
                    bootbox.alert("该账户已被停用，请联系管理员");
                }else if (data["errorCode"] == "2007") {
                    bootbox.alert("验证码错误，请重新输入");
                } else {
                    bootbox.alert("未知错误，请联系管理员");
                }
            }
        },
        error: function (data) {
            bootbox.alert("登录异常，请联系管理员");
        }
    });
}
